package cn.endowment.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import cn.endowment.pojo.ResultModel;


/**
 * 全局异常处理器
 * @author zsf
 *
 */

@RestControllerAdvice
public class GlobalExceptionHandler {
	
	/**
	 * 空指针异常
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(value = { NullPointerException.class })
	public final ResultModel<?> handleServiceException(Exception ex) {
		ResultModel<Object> rs = new ResultModel<Object>();
		rs.setCode(ErrorCode.http_innererr.getIndex());
		rs.setMsg(ErrorCode.http_innererr.getName());
		//TODO:记录异常信息到日志中
		return rs;
	}

	/**
	 * 自定义异常
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(value = { ServiceException.class })
	public final ResultModel<?> handleServiceException(ServiceException ex) {
		ResultModel<Object> rs = new ResultModel<Object>();
		rs.setCode(ex.getErr().getIndex());
		rs.setMsg(ex.getErr().getName());
		//TODO:记录异常信息到日志中
		return rs;
	}

}
